import 'package:flutter/material.dart';
import 'package:flutter_rider/main/widgets/ext/u_title_layout.dart';
import 'package:flutter_rider/main/widgets/state/refreshable_layout.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/module/mine/api/msg_api.dart';
import 'package:flutter_rider/module/mine/models/msg_record_model.dart';
import 'package:flutter_rider/module/mine/widgets/msg_widget.dart';
import 'package:flutter_rider/utils/screen_fit.dart';

class MessageDetailPage extends StatefulWidget {
  ///消息类别  1=车辆消息 2=运单消息 3=任务消息;
  final int type;
  const MessageDetailPage(this.type, {Key? key}) : super(key: key);

  @override
  State<MessageDetailPage> createState() => _MessageDetailPageState();
}

class _MessageDetailPageState extends State<MessageDetailPage> {
  final _stateController = RefreshableLayoutController();
  //请求id默认是0
  var _id = 0;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: UIColor.color_main,
        appBar: UTitleLayout.getBackTitle(
            context,
            widget.type == 1
                ? "运单消息"
                : widget.type == 2
                    ? "任务消息"
                    : "车辆消息"),
        body: SafeArea(
          child: _renderList(),
        ));
  }

  Widget _renderList() {
    return Container(
        margin: EdgeInsets.all(12.rpx),
        child: RefreshableLayout(
            controller: _stateController,
            urlCallback: _buildUrl,
            handleDataCallback: _handleData,
            singleDataCallback: _singleData,
            itemBuilder: _renderListItem));
  }

  String _buildUrl() {
    var baseUrl = "${MsgApi.msgRecord}?type=${widget.type}&id=$_id";
    return baseUrl;
  }

  _handleData(dynamic list) {
    return list.map((e) => MsgRecordModel.fromMap(e)).toList();
  }

  _singleData(dynamic data, bool isRefresh) {
    if (isRefresh) {
      var msg = data as MsgRecordModel;
      _id = msg.id;
    } else {
      _id = 0;
    }
  }

  Widget _renderListItem(dynamic data, int index) {
    return MsgSubWidget(data as MsgRecordModel);
  }
}
